<template>
  <div class="bg-ff">
    <div
      v-for="(item, index) in list"
      :key="index"
      class="padding-40 border-bottom flex justify-between align-center"
      @click="change(item.check, index)"
    >
      {{item.title}}
      <input class="scale" type="checkbox" :value="index" :checked="item.change" />
    </div>

    <div class="margin-t80">
      <div class="sure bg-ff0 text-ff text-center letter-10" @click="sure">保存</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      check: [],
      changeList: [] // 结果
    };
  },
  mounted() {
    document.querySelector('html').style.backgroundColor = "#ffffff";
    this.list = this.$route.params.list;
    this.check = this.$route.params.list;
    for (let i in this.list) {
      for (let j in this.check) {
        if (this.list[i].id == this.check[j]) {
          this.$set(this.list[i], "change", true);
        }
      }
    }
  },
  methods: {
    change(type, index) {
      this.list[index].change = !type;
      this.changeList = [];
      for (let i in this.list) {
        if (this.list[i].change) {
          this.changeList.push(this.list[i].id);
        }
      }
    },
    sure() {
      let type = this.$route.params.type;
      let value = this.changeList.length > 0 ? this.changeList : this.check;
      if(value.length == 0 && this.$route.params.notnull == 1){
        this.$toast('请选择完整~')
        return false
      }
      this.$axios.post( "/wap/personal/editUserInfo", {
        token: window.localStorage.getItem("token"),
        key: type,
        value: "," + value.join(",") + ","
      }).then(res => {
        this.$toast(res.data.msg);
        if (res.data.code == 0) {
          this.$router.go(-1);
        }
      });
    }
  }
};
</script>

<style>
html {
  background: white;
}
</style>